package org.lex.checksum.ui;

import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.SystemColor;

public class ProgressBar extends Canvas {
	private static final long serialVersionUID = 1L;
	// private int minimum = 0;
	private int progress = 0;
	private int maximum = 100;
	// =======================
	private Insets insets = null;

	public ProgressBar() {
		this.insets = new Insets(2, 2, 2, 2);
		this.setPreferredSize(new Dimension(Integer.MAX_VALUE, 11));
	}

	public int getProgress() {
		return this.progress;
	}

	public void setProgress(int progress) {
		this.progress = progress;
		this.repaint();
	}

	public void paint(Graphics g) {
		this.paintBorder(g);
		this.paintProgress(g);
	}

	private void paintBorder(Graphics g) {
		int height = this.getHeight();
		int width = this.getWidth();
		g.setColor(SystemColor.controlShadow);
		g.drawLine(1, 0, width - 1, 0);
		g.drawLine(0, 0, 0, height - 1);
		g.setColor(SystemColor.controlLtHighlight);
		g.drawLine(0, height - 1, width, height - 1);
		g.drawLine(width - 1, 0, width - 1, height - 1);
	}

	private void paintProgress(Graphics g) {
		int height = this.getHeight() - insets.top - insets.bottom;
		int width = this.getWidth() - insets.left - insets.right;
		width = width * progress / maximum;
		g.setColor(SystemColor.activeCaption);
		g.fillRect(insets.left, insets.top, width, height);
	}
}
